#include "DashboardUI_ViewPrescriptions.h"
#include "ui_dashboard_viewprescriptions.h"

#include "Authentication.h"
#include "PrescriptionDatabase.h"

DashboardUI_ViewPrescriptions::DashboardUI_ViewPrescriptions(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DashboardUI_ViewPrescriptions)
{
    ui->setupUi(this);

    reload();
}

void DashboardUI_ViewPrescriptions::reload()
{
    Authentication * auth = Authentication::getInstance();
    User * user = auth->getUser();

    int userId;
    if (user->getUserType() == User::PATIENT)
        userId = user->getId();
    else if (auth->getPatient() != nullptr)
        userId = auth->getPatient()->getId();
    else
        return;

    prescripDB.loadPrescriptions(userId);

    ui->prescriptionTableView->reset();
    ui->prescriptionTableView->setModel(prescripDB.getModel());
}

DashboardUI_ViewPrescriptions::~DashboardUI_ViewPrescriptions()
{
    delete ui;
}
